<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Aug 15, 2008
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "security.AbstractDOSDetect" );

class IpDOSDetect extends AbstractDOSDetect {
	
	var $_ip;
	var $_logFile;
	
	/**
	 * Constructor
	 *
	 * @param int $limitRequest
	 * @param int $timePerLimit
	 */
	function __construct($limitRequest = 10000, $timePerLimit = 60) {
		$this->_ip = $_SERVER [REMOTE_ADDR];
		$folder = str_replace ( ".", "/", $this->_ip );
		$this->_logFile = SYS_ROOT_PATH . "logs/dos/$folder/{$this->_ip}.dos.log";
		
		RCoreImporter::cls( "helper.RFileFolderHelper" );
		RFileFolderHelper::checkAndCreateFile ( $this->_logFile );
		
		parent::__construct ( $limitRequest, $timePerLimit );
	}
	
	/**
	 * load the start time and number of limitation from some where in "memory"
	 *
	 */
	function _loadLimitInfo() {
		$content = file_get_contents ( $this->_logFile );
		$arr = explode ( "__|__", $content );
		$this->_startTime = $arr [0];
		$this->_numberRequest = $arr [1];
	}
	
	/**
	 * save the start time and number of limitation into some where in "memory"
	 *
	 */
	function _saveLimitInfo() {
		$file = fopen ( $this->_logFile, 'w' );
		if ($file) {
			$content = "{$this->_startTime}__|__{$this->_numberRequest}";
			fwrite ( $file, $content );
			fclose ( $file );
		}
	}
}
?>